﻿using System;
using System.Xml.Serialization;

namespace Limestone.Fields
{
    [Serializable]
    public class FileField : Field, IField
    {
        public string FileUrl { get; set; }
        [XmlIgnore]
        public string FileName
        {
            get
            {
                var idx = FileUrl.LastIndexOf("/", System.StringComparison.Ordinal);
                return idx == -1 ? FileUrl : FileUrl.Substring(idx+1);
            }
        }

        public new FileFieldSetting Settings
        {
            get { return base.Settings as FileFieldSetting; }
            set { base.Settings = value; }
        }


        public FileField()
            : base(FieldType.FileField, "")
        {
            FileUrl = "";
        }
        public FileField(string name)
            : base(FieldType.FileField, name)
        {
            FileUrl = "";
        }


        public string GetHtml()
        {
            if (string.IsNullOrEmpty(FileUrl))
                return "";

            return "<a href=\"" + FileUrl + "\" >" + FileName + "</a>";
        }
    }
}
